<!DOCTYPE html>
<html>
<!--
Copyright 2012 The GlueBSF Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - glue.Cache</title>
<script src="../closure/goog/base.js"></script>
<script src="glue-deps.js"></script>
<script>
  	goog.require('glue.Cache');
  	
	goog.require('goog.testing.jsunit');
	goog.require('goog.object');
	goog.require('goog.async.Deferred');
	
</script>
</head>
<body>
<script>

var URL =  '/test/data/';

function getCache(){
	var cache = new glue.Cache({dispatchEvent:function(e){}});
	cache.clear()
	return cache;		
}

function testConstructor(){
	var cache =  getCache();
	
	assertEquals('testConstructor(), active_ should be empty initially', goog.object.getCount(cache.active_),0);
	assertEquals('testConstructor(), logics_ should be empty initially', goog.object.getCount(cache.logics_),0);
}

function testAddLogic(){
	var cache =  getCache();
	
	assertEquals('testAddLogic(), logics_ should be empty initially', goog.object.getCount(cache.logics_),0);
	
	cache.addLogic({});
	assertEquals('testAddLogic(), logics_ should be 1', goog.object.getCount(cache.logics_),1);
	
	for(var i = 0;i < 9;i++){
		cache.addLogic({});	
	}
	assertEquals('testAddLogic(), logics_ should be 10', goog.object.getCount(cache.logics_),10);
}

function testAccessors(){
	var cache =  getCache();
		
	cache.set(URL,{data:true});
	assertTrue('testAccessors(), Should contain the key',cache.containsKey(URL));
	assertTrue('testAccessors(), Should contain data that is true',cache.get(URL).data);
	
	cache.set(URL,undefined)
	assertFalse('testAccessors(), Should not contain the key',cache.containsKey(URL));
	
	cache.set(URL,{data:true});
	assertTrue('testAccessors(), Should contain the key',cache.containsKey(URL));
	assertTrue('testAccessors(), Should contain data that is true',cache.get(URL).data);
	
	cache.clear();
	
	assertFalse('testAccessors(), Should not contain the key',cache.containsKey(URL));	
	assertEquals('testAccessors(), active_ should be empty', goog.object.getCount(cache.active_),0);
}

function testSetHolder(){
	var cache =  getCache();
		
	var deferred = cache.setHolder(URL);
	assertTrue('testSetHolder(), should be true',deferred instanceof goog.async.Deferred)
	assertFalse('testAccessors(), Should not contain the key',cache.containsKey(URL));
	
	var deferred = cache.setHolder(URL,true);
	assertTrue('testSetHolder(), should be true',deferred instanceof goog.async.Deferred)
	assertTrue('testAccessors(), Should not contain the key',cache.containsKey(URL));
	
	cache.clear()
	var deferred = cache.setHolder(URL,false);
	assertTrue('testSetHolder(), should be true',deferred instanceof goog.async.Deferred)
	assertFalse('testAccessors(), Should not contain the key',cache.containsKey(URL));
}

function testRevalidation(){
	var cache =  getCache();
		
	cache.set(URL,{data:true});
	assertTrue('testRevalidation(), Should contain the key',cache.containsKey(URL));
	assertTrue('testRevalidation(), Should contain data that is true',cache.get(URL).data);
	
	cache.conditionalRevalidate(URL,function(data){
		assertTrue('testRevalidation(), Should contain data that is true',data.data);
		data.data = false
		assertFalse('testRevalidation(), Should contain data that is true',data.data);
		return data;	
	});
	assertFalse('testRevalidation(), Should contain data that is true',cache.get(URL).data);
		
	cache.setCacheValidation(glue.Cache.ValidationFunctions.noCache)
	cache.revalidate()
	assertFalse('testRevalidation(), Should contain the key',cache.containsKey(URL));
	
}

</script>
</body>
</html>
